<template>
  <div class="form-item-input" :class="classStyle">
    <input
      v-if="type!='number'&&maxLength!='11'"
      :type="type"
      v-model="val"
      :maxlength="maxLength"
      :placeholder="placeholder"
      @blur="blur"
      @focus="focus"
    >
    <input
      v-if="type==='number'"
      onkeypress="return(/[\d]/.test(String.fromCharCode(event.keyCode)))"
      oninput="if(value.length>11)value=value.slice(0,11)"
      type="number"
      v-model="val"
      @blur="blur"
      :placeholder="placeholder"
      @focus="focus"
    >
    <div
      :class="['form-item-input__code',codeRetry?'form-item-input__code__retry':'']"
      v-if="showCode"
      @click="getCode"
    >
      <!-- <slot name="code"></slot> -->
      {{text}}
    </div>
  </div>
</template>

<script>
export default {
  name: "KInput",
  props: {
    type: String,
    value: [Number, String],
    placeholder: String,
    showCode: Boolean,
    maxLength: {
      type: String,
      default: "50"
    },
    text: {
      type: String,
      default: "获取验证码"
    },
    codeRetry: false,
    codeDisabled: Boolean
  },
  computed: {
    val: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit("input", val);
        this.$emit("change", val);
      }
    },
    classStyle() {
      return [this.codeDisabled && "form-item-input__loading"];
    }
  },
  methods: {
    getCode(e) {
      if (!this.codeDisabled) {
        this.$emit("getCode", e);
      }
    },
    blur(e) {
      this.keyboardHidden();
      this.$emit("blur", e);
      if (this.$parent.prop) {
        // this.$parent.$emit('k.form.blur', this.val);
      }
    },
    focus() {
      this.keyboardShow();
    },
    keyboardHidden() {
      const arr = navigator.userAgent
        .toLowerCase()
        .match(/cpu iphone os (.*?) like mac os/);
      var iosV = 0;
      if (arr && arr.length) {
        iosV = parseInt(arr[1].replace(/_/g, "."));
      }
      if (iosV < 12) return;
      this.keyboardState = false;
      var currentPosition;
      var speed = 1; //页面滚动距离
      setTimeout(() => {
        if (!this.keyboardState) {
          currentPosition =
            document.documentElement.scrollTop || document.body.scrollTop;
          currentPosition -= speed;
          window.scrollTo(0, currentPosition); //页面向上滚动
          currentPosition += speed; //speed变量
          window.scrollTo(0, currentPosition); //页面向下滚动
        }
      }, 100);
    },
    keyboardShow() {
      this.keyboardState = true;
    }
  }
};
</script>

<style lang="less">
.form-item-input {
  position: relative;
  input {
    box-sizing: border-box;
    border: 1px solid #d6d6d6;
    outline: none;
    width: 100%;
    height: 44px;
    padding-left: 12px;
    font-size: 14px;
    -webkit-appearance: none;
    border-radius: 0;
  }

  input::placeholder {
    color: #999;
    font-size: 14px;
  }
  input:focus {
    color: #000;
    border-color: #000;
  }

  &__code {
    color: #000;
    font-size: 14px;
    min-width: 85px;
    text-align: right;
    padding-right: 14px;
    position: absolute;
    right: 0;
    bottom: 50%;
    transform: translateY(50%);
    &:before {
      content: "";
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      display: inline-block;
      height: 18px;
      width: 1px;
      background: #d6d6d6;
    }
  }
  &__code__retry {
    min-width: 95px;
  }
}

.form-item-input {
  &__loading {
    .form-item-input__code {
      color: #999;
      height: 42px;
      line-height: 42px;
    }
  }
}
</style>
